<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>Shader-2D-流水效果 | NoGameNoLife</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="Shader2D - 流水效果1. 效果分析流水效果可以使用微小的的圆形Sprite模拟水滴，通过大量克隆来模拟流水效果。但是在每个圆形Sprite中间会有很大的空隙，这就是我们需要主要解决的问题——消除圆形Sprite之间的空隙使其看上去像流水。 流水效果的实现分为两个部分，第一个部分为模糊操作，第二个部分是阈值划分。模糊操作的目的是将圆形Sprite的范围扩大，使得圆形Sprite之间原本空白">
<meta property="og:type" content="article">
<meta property="og:title" content="Shader-2D-流水效果">
<meta property="og:url" content="http://example.com/2021/02/22/Shader-2D-%E6%B5%81%E6%B0%B4%E6%95%88%E6%9E%9C/index.html">
<meta property="og:site_name" content="NoGameNoLife">
<meta property="og:description" content="Shader2D - 流水效果1. 效果分析流水效果可以使用微小的的圆形Sprite模拟水滴，通过大量克隆来模拟流水效果。但是在每个圆形Sprite中间会有很大的空隙，这就是我们需要主要解决的问题——消除圆形Sprite之间的空隙使其看上去像流水。 流水效果的实现分为两个部分，第一个部分为模糊操作，第二个部分是阈值划分。模糊操作的目的是将圆形Sprite的范围扩大，使得圆形Sprite之间原本空白">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-02-22T05:36:27.000Z">
<meta property="article:modified_time" content="2021-02-23T01:58:31.549Z">
<meta property="article:author" content="林文豪">
<meta property="article:tag" content="-Shader">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="NoGameNoLife" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 5.1.1"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">NoGameNoLife</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-Shader-2D-流水效果" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2021/02/22/Shader-2D-%E6%B5%81%E6%B0%B4%E6%95%88%E6%9E%9C/" class="article-date">
  <time datetime="2021-02-22T05:36:27.000Z" itemprop="datePublished">2021-02-22</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/Shader/">-Shader</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Shader-2D-流水效果
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="Shader2D-流水效果"><a href="#Shader2D-流水效果" class="headerlink" title="Shader2D - 流水效果"></a>Shader2D - 流水效果</h3><h4 id="1-效果分析"><a href="#1-效果分析" class="headerlink" title="1. 效果分析"></a>1. 效果分析</h4><p>流水效果可以使用微小的的圆形Sprite模拟水滴，通过大量克隆来模拟流水效果。但是在每个圆形Sprite中间会有很大的空隙，这就是我们需要主要解决的问题——消除圆形Sprite之间的空隙使其看上去像流水。</p>
<p>流水效果的实现分为两个部分，第一个部分为模糊操作，第二个部分是阈值划分。模糊操作的目的是将圆形Sprite的范围扩大，使得圆形Sprite之间原本空白的区域填充上带有透明度变化的像素。阈值划分的目的是控制区域的颜色以及颜色的范围。</p>
<p>综上，我们一共需要两个Shader，一个Shader负责进行模糊操作，一个Shader负责阈值划分。</p>
<h4 id="2-模糊操作"><a href="#2-模糊操作" class="headerlink" title="2. 模糊操作"></a>2. 模糊操作</h4><p>这里的模糊操作我采用的是《UnityShader入门精要》中提到的高斯模糊，当然也可以用平均模糊，模糊的方法是自定义的。</p>
<h4 id="3-阈值划分"><a href="#3-阈值划分" class="headerlink" title="3. 阈值划分"></a>3. 阈值划分</h4><p>第一种方法是自己设定阈值的范围，比如透明度小于0.1的剔除、透明度在0.1到0.5之间的用第一种颜色、其余部分用第二种颜色。</p>
<p>第二种方法是用一个数学公式代替显式的规定阈值，例如$floor(col.a * 10) * 0.5$ ，其实这个公式也是一个对阈值的划分，只不过是用数学公式来表示。</p>
<h4 id="4-具体实现"><a href="#4-具体实现" class="headerlink" title="4. 具体实现"></a>4. 具体实现</h4><p>模糊部分：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">v2f vertHorizontal (appdata v)</span><br><span class="line">         &#123;</span><br><span class="line">             v2f o;</span><br><span class="line">             o.vertex &#x3D; UnityObjectToClipPos(v.vertex);</span><br><span class="line">             o.uv[0] &#x3D; v.uv;</span><br><span class="line">	o.uv[1] &#x3D; v.uv + float2(_MainTex_TexelSize.x * 1.0, 0.0) * _BlurSize;</span><br><span class="line">	o.uv[2] &#x3D; v.uv - float2(_MainTex_TexelSize.x * 1.0, 0.0) * _BlurSize;</span><br><span class="line">	o.uv[3] &#x3D; v.uv + float2(_MainTex_TexelSize.x * 2.0, 0.0) * _BlurSize;</span><br><span class="line">	o.uv[4] &#x3D; v.uv - float2(_MainTex_TexelSize.x * 2.0, 0.0) * _BlurSize;</span><br><span class="line">             return o;</span><br><span class="line">         &#125;</span><br><span class="line"></span><br><span class="line">         v2f vertVertical (appdata v)</span><br><span class="line">         &#123;</span><br><span class="line">             v2f o;</span><br><span class="line">             o.vertex &#x3D; UnityObjectToClipPos(v.vertex);</span><br><span class="line">             o.uv[0] &#x3D; v.uv;</span><br><span class="line">	o.uv[1] &#x3D; v.uv + float2(0, _MainTex_TexelSize.y * 1.0) * _BlurSize;</span><br><span class="line">	o.uv[2] &#x3D; v.uv - float2(0, _MainTex_TexelSize.y * 1.0) * _BlurSize;</span><br><span class="line">	o.uv[3] &#x3D; v.uv + float2(0, _MainTex_TexelSize.y * 2.0) * _BlurSize;</span><br><span class="line">	o.uv[4] &#x3D; v.uv - float2(0, _MainTex_TexelSize.y * 2.0) * _BlurSize;</span><br><span class="line">             return o;</span><br><span class="line">         &#125;</span><br><span class="line"></span><br><span class="line">         fixed4 frag (v2f i) : SV_Target</span><br><span class="line">         &#123;</span><br><span class="line">	float weight[3] &#x3D; &#123;0.4026, 0.2442, 0.0545&#125;;</span><br><span class="line"></span><br><span class="line">             fixed4 col &#x3D; tex2D(_MainTex, i.uv[0]) * weight[0];</span><br><span class="line">	for(int it&#x3D;1;it&lt;3;it++)&#123;</span><br><span class="line">		col +&#x3D; tex2D(_MainTex, i.uv[it * 2 - 1]) * weight[it];</span><br><span class="line">		col +&#x3D; tex2D(_MainTex, i.uv[it * 2]) * weight[it];</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">             return col;</span><br><span class="line">         &#125;</span><br></pre></td></tr></table></figure>

<p>阈值划分，数学公式：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">fixed4 c &#x3D; tex2D(_MainTex, i.uv);</span><br><span class="line">return floor(c*10)*0.5;</span><br></pre></td></tr></table></figure>

<p>阈值划分，显示设置透明度：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">fixed4 c &#x3D; tex2D(_MainTex, i.uv);</span><br><span class="line">clip(c.a - _Cutoff);</span><br><span class="line">if(c.a &lt; _Stroke)&#123;</span><br><span class="line">	c &#x3D; _StrokeColor;</span><br><span class="line">&#125;</span><br><span class="line">else &#123;</span><br><span class="line">	c &#x3D; _Color;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">            return c;</span><br></pre></td></tr></table></figure>

<h4 id="5-注意事项"><a href="#5-注意事项" class="headerlink" title="5. 注意事项"></a>5. 注意事项</h4><ul>
<li>Unity2D的相机设置 <a href="/2021/01/30/%E5%AD%A6%E4%B9%A0Shader%E5%85%A5%E9%97%A8%E7%B2%BE%E8%A6%81%E7%9A%84%E8%AE%B0%E5%BD%95/" title="学习Shader入门精要的记录">学习Shader入门精要的记录</a></li>
<li>模糊效果中的优化</li>
<li>模糊效果中的卷积</li>
<li>与Image Effect 中的BlurEffect的区别</li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2021/02/22/Shader-2D-%E6%B5%81%E6%B0%B4%E6%95%88%E6%9E%9C/" data-id="cklhcy2mh00011kdl3xx9cjru" class="article-share-link">分享</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Shader/" rel="tag">-Shader</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2021/02/23/Unity%E5%9F%BA%E7%A1%80-2D%E7%9B%B8%E6%9C%BA%E6%B8%B2%E6%9F%93%E5%88%B0RT%E7%9A%84%E7%9B%B8%E5%85%B3%E8%AE%BE%E7%BD%AE/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Unity基础-2D相机渲染到RT的相关设置
        
      </div>
    </a>
  
  
    <a href="/2021/01/30/%E5%AD%A6%E4%B9%A0Shader%E5%85%A5%E9%97%A8%E7%B2%BE%E8%A6%81%E7%9A%84%E8%AE%B0%E5%BD%95/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">学习Shader入门精要的记录</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Shader/">-Shader</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Artifical-Intelligence/">Artifical Intelligence</a><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/Artifical-Intelligence/NPC-Behavior-Decision-making/">NPC Behavior Decision-making</a><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/Artifical-Intelligence/NPC-Behavior-Decision-making/GOAP/">GOAP</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Artifical-Intelligence/NPC-Behavior-Decision-making/Utility-based-System/">Utility-based System</a></li></ul></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/Shader/">Shader</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Unity-Mesh/">Unity-Mesh</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Unity%E5%9F%BA%E7%A1%80/">Unity基础</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">标签</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Shader/" rel="tag">-Shader</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/2D%E7%9B%B8%E6%9C%BA%E8%AE%BE%E7%BD%AE/" rel="tag">2D相机设置</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Artifical-Intelligence/" rel="tag">Artifical Intelligence</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/GOAP/" rel="tag">GOAP</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Unity-Mesh/" rel="tag">Unity-Mesh</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Utility-based-System/" rel="tag">Utility-based System</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E2%80%94-Shader/" rel="tag">— Shader</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">标签云</h3>
    <div class="widget tagcloud">
      <a href="/tags/Shader/" style="font-size: 10px;">-Shader</a> <a href="/tags/2D%E7%9B%B8%E6%9C%BA%E8%AE%BE%E7%BD%AE/" style="font-size: 10px;">2D相机设置</a> <a href="/tags/Artifical-Intelligence/" style="font-size: 20px;">Artifical Intelligence</a> <a href="/tags/GOAP/" style="font-size: 15px;">GOAP</a> <a href="/tags/Unity-Mesh/" style="font-size: 10px;">Unity-Mesh</a> <a href="/tags/Utility-based-System/" style="font-size: 10px;">Utility-based System</a> <a href="/tags/%E2%80%94-Shader/" style="font-size: 10px;">— Shader</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/02/">二月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/01/">一月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/12/">十二月 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/02/24/Unity-Mesh-%E7%BB%98%E5%88%B6%E6%AD%A3%E6%96%B9%E4%BD%93/">Unity-Mesh-绘制正方体</a>
          </li>
        
          <li>
            <a href="/2021/02/23/Unity%E5%9F%BA%E7%A1%80-2D%E7%9B%B8%E6%9C%BA%E6%B8%B2%E6%9F%93%E5%88%B0RT%E7%9A%84%E7%9B%B8%E5%85%B3%E8%AE%BE%E7%BD%AE/">Unity基础-2D相机渲染到RT的相关设置</a>
          </li>
        
          <li>
            <a href="/2021/02/22/Shader-2D-%E6%B5%81%E6%B0%B4%E6%95%88%E6%9E%9C/">Shader-2D-流水效果</a>
          </li>
        
          <li>
            <a href="/2021/01/30/%E5%AD%A6%E4%B9%A0Shader%E5%85%A5%E9%97%A8%E7%B2%BE%E8%A6%81%E7%9A%84%E8%AE%B0%E5%BD%95/">学习Shader入门精要的记录</a>
          </li>
        
          <li>
            <a href="/2020/12/29/%E4%B8%AA%E4%BA%BA%E5%AF%B9Utility-based-System%E7%90%86%E8%A7%A3/">个人对Utility-based System的理解</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2021 林文豪<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>